package com.inscream.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.inscream.app.beforeLogin.ForgotPassword;
import com.inscream.app.beforeLogin.RegisterActivity;


public class LoginActivity extends Activity {

    Button loginButton, getRegister;
    SharedPreferences preferences;
    String myUsername;
    String isHasUsername;
    EditText usernameText, passwordText;
    TextView forgotPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        boolean InternetConnection = haveNetworkConnection();
        if(InternetConnection == true){
        forgotPassword = (TextView) findViewById(R.id.forgotPassword);
        forgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent forgotPass = new Intent(LoginActivity.this, ForgotPassword.class);
                startActivity(forgotPass);
            }
        });
        usernameText = (EditText) findViewById(R.id.usernameText);
        passwordText = (EditText) findViewById(R.id.passwordText);
        // Kullanıcı eğer oturum açmışsa sharedPreferences dolu ve adam direkt MainActivity'e girebiliyor
        // Daha önce girdiğinde sharedPreferences set etmişiz, logout demedikçe de silmiyoruz
        myUsername = "mehmetakifalp";
        preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("myUsername", myUsername);
        editor.commit();
        isHasUsername = preferences.getString("myUsername", "N/A");
        if(isHasUsername.toString() == null ){
            Toast.makeText(getApplicationContext(), "Daha önce girmemiş", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Daha önce girmiş direkt activity açıldı", Toast.LENGTH_SHORT).show();
        }
        getRegister = (Button) findViewById(R.id.getRegister);
        loginButton = (Button) findViewById(R.id.button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // bu ya da bu dolu gelirse, 2 sininde 5 karakterden büyük olduğundan emin ol, doğruysa çalış, değilse if ile hangisi küçük onu tespit et ona hata mesajı düş
                if(!usernameText.getText().toString().matches("") || !passwordText.getText().toString().matches("")){
                    // Toast.makeText(getApplicationContext(), "2 sinden BİRİ dolu geldi", Toast.LENGTH_SHORT).show();
                    // 5 karakterden küçük değilse
                    if(!(usernameText.getText().toString().length() < 5) && !(passwordText.getText().toString().length() < 5)){
                         // Toast.makeText(getApplicationContext(), "2 si de 5 karakterden büyük", Toast.LENGTH_SHORT).show();
                         Intent i = new Intent(LoginActivity.this, MainActivity.class);
                         startActivity(i);
                         // 5 karakterden küçükse
                    } else {
                         // Toast.makeText(getApplicationContext(), "2si Boş değil ama 5 karakterden küçük", Toast.LENGTH_SHORT).show();
                        if((usernameText.getText().toString().length() < 5)){
                            usernameText.setError(usernameText.getHint()+" 5 karakterden küçük olamaz");
                        } else {
                            usernameText.setError(null);
                        }
                        if ((passwordText.getText().toString().length() < 5)){
                            passwordText.setError(passwordText.getHint()+" 5 karakterden küçük olamaz");
                        } else{
                            passwordText.setError(null);
                        }
                    }
                } else {
                    if(usernameText.getText().toString().matches("")){
                        usernameText.setError(usernameText.getHint()+" must be filled");
                    }
                    if (passwordText.getText().toString().matches("")){
                        passwordText.setError(passwordText.getHint()+" must be filled");
                    }
                }
            }
        });
        getRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent registerActivity = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(registerActivity);
            }
        });
        } else {
            Toast.makeText(getApplicationContext(), "You dont have internet connection", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean haveNetworkConnection(){
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
